package com.onpositive.wictionary.access;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringReader;
import java.util.HashSet;

import com.onpositive.wiki2.whole.model.ContentAccess;

public class VocubularyAccess {

	ContentAccess access;
	private String path;
	private SemanticVocubalary v;

	public String getContent(int id) throws IOException {
		return access.getContent(id);
	}

	public String getContent(String title) throws IOException {
		return access.getContent(title);
	}

	public String getTitle(int a) {
		return access.getTitle(a);
	}

	public VocubularyAccess(String path) throws FileNotFoundException,
			ClassNotFoundException, IOException {
		this.access = new ContentAccess(path);
		this.path = path;
	}

	public int size() {
		return access.size();
	}

	public SemanticVocubalary getVocubalary() {
		try {
			if (v != null) {
				return v;
			}
			File fs = new File(path + "/voc.obj");
			if (fs.exists()) {
				ObjectInputStream os = new ObjectInputStream(
						new BufferedInputStream(new FileInputStream(fs)));
				v = (SemanticVocubalary) os.readObject();
				return v;
			}
			SemanticVocubalary filllThesaurus = filllThesaurus(this);
			ObjectOutputStream os = new ObjectOutputStream(
					new BufferedOutputStream(new FileOutputStream(fs)));
			os.writeObject(filllThesaurus);
			os.close();
			v = filllThesaurus;
			return filllThesaurus;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static SemanticVocubalary filllThesaurus(
			VocubularyAccess vocubularyAccess) throws IOException {
		SemanticVocubalary vv = new SemanticVocubalary();
		for (int a = 0; a < vocubularyAccess.size(); a++) {
			String title = vocubularyAccess.getTitle(a);
			if (title.startsWith("Wikisaurus:")) {
				VocubalaryEntry s = new VocubalaryEntry(
						title.substring("Wikisaurus:".length()));
				String content = vocubularyAccess.getContent(a);
				BufferedReader dd = new BufferedReader(
						new StringReader(content));
				HashSet<String> clist = null;
				while (true) {
					String readLine = dd.readLine();
					if (readLine == null) {
						break;
					}
					if (readLine.contains("==")) {
						if (readLine.contains("Synonyms")) {
							clist = s.synonims;
							continue;
						}
						if (readLine.contains("Hyponyms")) {
							clist = s.hyponims;
							continue;
						}
						if (readLine.contains("Hypernyms")) {
							clist = s.hypernims;
							continue;
						}
						if (readLine.contains("Meronyms")) {
							clist = new HashSet<String>();
							continue;
						}
						if (readLine.contains("Holonyms")) {
							clist = new HashSet<String>();
							continue;
						}
						clist = null;
					}
					if (readLine.startsWith("{{ws|")) {
						String string = readLine.substring(5,
								readLine.length() - 2);
						int indexOf = string.indexOf('}');
						if (indexOf != -1) {
							string = string.substring(0, indexOf);
						}
						indexOf = string.indexOf('|');
						if (indexOf != -1) {
							string = string.substring(0, indexOf);
						}
						if (clist != null) {
							clist.add(string);
						}
						// System.out.println(string);

					}
				}
				vv.add(s);
			}
		}
		for (int a = 0; a < vocubularyAccess.size(); a++) {
			String title = vocubularyAccess.getTitle(a);
			String content = vocubularyAccess.getContent(a);
			BufferedReader dd = new BufferedReader(new StringReader(content));
			while (true) {
				String readLine = dd.readLine();
				if (readLine == null) {
					break;
				}
				if (readLine.startsWith("{{en-noun")) {
					String str = readLine.substring("{{en-noun".length());
					int indexOf = str.indexOf('}');
					if (indexOf != -1) {
						str = str.substring(0, indexOf);
					}
					String[] split = str.split("\\|");
					for (String s : split) {
						int ll = s.indexOf('=');
						if (ll != -1) {
							String k = s.substring(0, ll);
							String v = s.substring(ll + 1);
							if (k.startsWith("pl")) {
								vv.recordSinglePlural(title, v);
							}
						}
					}

				}
			}
		}
		return vv;
	}

}
